package com.example.demo.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.User;
import com.xphsc.easyjdbc.page.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.expression.Lists;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
public class UserController extends BaseController {

    @Autowired
    UserMapper userMapper;

    @PostMapping(value = "/login")
    @ResponseBody
    public Object login(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password, HttpServletResponse response, HttpServletRequest request) throws IOException {
        User user = userMapper.findByUserName(username);
        if (ObjectUtil.isNotEmpty(user)) {
            log.debug("用户存在");
            if (StrUtil.equals(password, password)) {
                log.debug("密码正确");
                request.getSession().setAttribute("userInfo", user);
                return ok();
            }
        }
        return error();
    }

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @GetMapping("/user/add")
    public String add() {
        return "add";
    }



    @PostMapping("/user/add")
    public void add(User user, HttpServletResponse response) throws IOException {
        log.info(JSONUtil.toJsonStr(user));
        if (user.getId() != null) {
            userMapper.update(user);
        }else{

            userMapper.save(user);
        }
        response.sendRedirect("/home");
    }


    @PostMapping("/user/all")
    @ResponseBody
    public Object all(Integer pageNum, Integer pageSize, String type) {

        Integer offset = page2Limit(pageNum);

//        ArrayList<User> rows = CollUtil.newArrayList();
//        List<User> list = userMapper.findList(offset, pageSize, type);
//        long count = userMapper.findCount(offset, pageSize, type);

        PageInfo<User> page = userMapper.findPage(offset, pageSize, type);
        Dict dict = new Dict().set("total", page.total).set("rows", page.list);
        return JSONUtil.toJsonStr(dict);
    }


    @GetMapping("/user/update")
    public String add(long userid, Model model) throws IOException {
        User user = userMapper.findById(userid);
        model.addAttribute("user", user);
        return "/update";

    }

    @PostMapping("/user/del")
    @ResponseBody
    public Object del(String userid) throws IOException {
        userMapper.del(userid);
        return ok();
    }

}
